package com.sirdc.modules.paas.api.mo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.paas.api.common.CommonResponse;
import com.sirdc.modules.paas.api.common.Constants;
import com.sirdc.modules.paas.api.common.util.CommonUtil;
import com.sirdc.modules.paas.api.common.util.XmlParseUtil;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 接收短信请求返回类.
 * 
 * @author Qing
 * @e-mail lhq@suncco.com
 * @version v1.0
 * @copyright 2010-2015
 * @create-time 2013-9-18 下午5:39:13
 * 
 */
public class MoResponse extends CommonResponse {

	private static final long serialVersionUID = -3129904959373364401L;
	
	private static final String MOAMOUNT = "moAmount";
	
	/**
	 * 成功接收短信的条数.
	 */
	private String moAmount;
	/**
	 * 接收短信的结果集.
	 */
	private List<MoInfo> moList;
	
	/**
	 * 解析请求返回的xml数据,设置返回对象的属性值.
	 * @param xmlStr 请求返回的xml字符串.
	 */
	public MoResponse(String xmlStr) {
		Map<String, String> map = super.getMapByXmlStr(xmlStr);
		if (map.containsKey(Constants.STATECODE)) {
			this.stateCode = map.get(Constants.STATECODE);
		}
		if (map.containsKey(Constants.STATEINTRO)) {
			this.stateIntro = map.get(Constants.STATEINTRO);
		}
		if ( map.containsKey(MOAMOUNT)) {
			this.moAmount = map.get(MOAMOUNT);
			this.xmlParseToMoList(xmlStr);
		}
	}

	/**
	 * 解析请求返回的xml数据,设置接收短信对象列表的属性值.
	 * @param xmlStr 请求返回的xml字符串.
	 */
	private void xmlParseToMoList(String xmlStr) {
	    moList=new ArrayList<MoInfo>();
		String tagName = "mo";
		NodeList nodeList = XmlParseUtil.getNodeListByTagName(XmlParseUtil.parseDoc(xmlStr, Constants.CHARSET), tagName);
		for (int i = 0; i < nodeList.getLength(); ++i) {
			Node node = nodeList.item(i);
			NodeList childNodeList = node.getChildNodes();
			MoInfo mo = new MoInfo();
			for (int j = 0; j < childNodeList.getLength(); ++j) {
				Node childNode = childNodeList.item(j);
				if (childNode.getNodeName().equals("sn")) {
					mo.setSn(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("smsId")) {
					mo.setSmsId(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("spNumber")) {
					mo.setSpNumber(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("phoneNumber")) {
					mo.setPhoneNumber(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("content")) {
					mo.setContent(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("createTime")) {
					mo.setCreateTime(CommonUtil.trimString(childNode.getTextContent()));
				}
			}
			moList.add(mo);
		}
	}

	public String getMoAmount() {
		return moAmount;
	}

	public void setMoAmount(String moAmount) {
		this.moAmount = moAmount;
	}

	public List<MoInfo> getMoList() {
		return moList;
	}

	public void setMoList(List<MoInfo> moList) {
		this.moList = moList;
	}

}
